package homework1;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 使用Future的get方法来获取返回值
 * 使用FutureTask
 */
public class ByFutureTask {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        // 开始时间
        long start=System.currentTimeMillis();

        // 1. 使用FutureTask
        // 新建一个线程
        FutureTask<Integer> task = new FutureTask<Integer>(ByFutureTask::sum);
        Thread thread = new Thread(task);
        thread.start();

        // 获取线程返回值
        int result = task.get();
        System.out.println("计算结果为："+result);
        System.out.println("使用时间："+ (System.currentTimeMillis()-start) + " ms");
        thread.join();
        System.out.println("退出主线程");
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if ( a < 2)
            return 1;
        return fibo(a-1) + fibo(a-2);
    }
}

